!(function(glob, undefined) {
    var option = {
        dom: {
            parent: "",
            child1: "",
            c1child1: "",
            child2: "",
            transit:"" //过度元素dom
        },
        animateTime: 500,//过度持续时间
        transitH: 30,//过度时需要超过的高度条件, 若有过度元素dom,则优先考虑过度元素dom的高度
        transitText: {//过度元素内所要显示的文案
            start: "第一屏开始",
            move1: "第一屏正在拖拽",
            move2:"第二屏正在拖拽",
            end: "第二屏开始"
        }
    };
    function MySwipe(opts) {
        option = opts;
    }
    //判断是否有过度元素
    function isTransit() {
        if(option.dom.transit == "" && typeof (option.dom.transit) != 'object'){
            return false;
        }
        return true;
    }
    //元素添加事件
    function  addEvent(el, type, fn) {
        el.addEventListener(type, fn, false);
    }
    //元素删除事件
    function clearEvent(el, type, fn) {
        var fn = fn ? fn : function (e) {
            console.log('默认方法');
        }
        el.removeEventListener(type, fn, false);
    }
    var Y1 = 0, sY1 = 0, Y2 = 0, sY2 = 0, styleInit = "", dom, c1Top, c1Height, transH;

    /***第一屏元素 start**/
    function  ft1start(e) {
        Y1 = e.touches[0].pageY;
        //dom.child2.style.display = "none";
        addEvent(dom.child1, 'touchmove', ft1move);
        addEvent(dom.child1, 'touchend', ft1end);
    }
    function  ft1move(e) {
        if(dom.child1.scrollTop == c1Top & sY1 ==0) {
            sY1 = e.touches[0].pageY;
        }
        Y1 = e.touches[0].pageY;
        if(sY1 > 0) {
            var trans = sY1 - Y1;
            if (trans >0){
                dom.parent.style =styleInit + "transform: translateY("+(-trans)+"px)";
                if(isTransit()){
                    dom.transit.innerHTML = option.transitText.move1;
                }
            }

        }
    }
    function  ft1end(e){
        var trans = sY1- Y1;
        if(trans > transH && sY1 > 0) {
            //dom.child2.style.display = "block";
            //c2Height= children2.offsetHeight;
            dom.parent.style =styleInit + "transform: translateY("+(-c1Height - transH)+"px);";
            document.body.scrollTop =0;
            if(isTransit()){
                dom.transit.innerHTML = option.transitText.end;
            }
        }else {
            father.style = styleInit;
        }
        sY1 = 0;
        Y1 =0;
        clearEvent(dom.child1, 'touchmove');
        clearEvent(dom.child1, 'touchend');
    }
    /***第一屏元素 end**/



    /**第二屏元素 start**/
    function ft2start(e) {
        Y2 = e.touches[0].pageY;
        addEvent(dom.child2, 'touchmove', ft2move);
        addEvent(dom.child2, 'touchend', ft2end);
    }
    function ft2move(e) {
        if(dom.child2.scrollTop <= 0 && sY2 == 0){
            sY2 = e.touches[0].pageY;
        }
        Y2 = e.touches[0].pageY;
        if(sY2 > 0) {
            var trans = Y2 -sY2;
            if(trans > 0) {
                dom.parent.style =styleInit + "transform: translateY("+(-c1Height - transH + trans)+"px)";
                if(isTransit()){
                    dom.transit.innerHTML = option.transitText.move2;
                }
            }
        }
    }
    function ft2end(e) {
        var trans = Y2 - sY2;
        if(trans > transH && sY2 > 0) {
            //dom.child2.style.display = "none";
            dom.parent.style =styleInit;
            if(isTransit()){
                dom.transit.innerHTML = option.transitText.start;
            }
        }else {
            dom.parent.style =styleInit + "transform: translateY("+(-c1Height - transH)+"px)";
        }
        sY2 = 0;
        Y2 =0;
        clearEvent(dom.child2, 'touchmove', ft2move);
        clearEvent(dom.child2, 'touchend', ft2end);
    }
    /**第二屏元素end***/

    MySwipe.prototype = {
        construct: MySwipe,
        init: function () {//初始化
            dom = option.dom;
            c1Height = dom.child1.offsetHeight;
            c1Top = dom.c1child1.offsetHeight - c1Height;
            transH = isTransit()? dom.transit.offsetHeight : option.transitH;
            styleInit = 'height:' + c1Height +"px;" +"transform: translateY(0); transition: transform "+option.animateTime/1000+"s cubic-bezier(0, 0, 0.25, 1) 0ms;";
            addEvent(dom.child1, "touchstart", ft1start);
            addEvent(dom.child2, "touchstart", ft2start);
        }
    }

    glob.MySwipe = MySwipe;
})(window);